var router = require('koa-router')({ prefix: '/project/:id' });
var models = require('./../../../models');
var q = require('q');
require('console.table');
/** 项目的测距仪配置 */
router.get('/angleconfig', function* (next) {
    var devices = yield this.state.project.getAngleDeviceConfigs();
    this.render('project/angle.config.jade', {
        url: this.url,
        project: this.state.project,
        devices: devices,
        angleCom: this.session.angleCom || 'COM1'
    });
});

router.post('/angleconfig', function* (next) {
    var body = this.request.body;
    yield models.AngleDeviceConfig.create(body);
    this.redirect(`/project/${this.params.id}/angleconfig`);
});

router.get('/angleconfig/:deviceUId/delete', function* (next) {
    var device = yield models.AngleDeviceConfig.findById(this.params.deviceUId);
    yield device.destroy();
    this.redirect(`/project/${this.params.id}/angleconfig`);
});

router.get('/angleconfig/:deviceUId/test/:com', function* (next) {
    this.session.angleCom = this.params.com;
    var device = yield models.AngleDeviceConfig.findById(this.params.deviceUId);
    this.state.device = device;
    var devices = yield this.state.project.getAngleDeviceConfigs();
    yield this.state.pipeCtrl.initialAngle(this.params.com, devices);
    var data = yield this.state.pipeCtrl.angleHelper.angleCom.survey(device.deviceId);
    yield this.state.pipeCtrl.angleHelper.closePort();
    if (data && (data.x !== null || data.y !== null)) {
        console.table([data]);
        this.body = data;
    } else {
        this.body = false;
    }
});
module.exports = router.routes();
